package com.tiandao.mofang.core.shicai;

import com.tiandao.mofang.core.RobotSingleton;
import com.tiandao.mofang.ui.MFFrame;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 行为模仿:驻地捡食材
 */
public class ZhuDi implements Runnable{
        private static Robot robot = RobotSingleton.getInstance();


        //类加载时读取坐标配置文件
        private static File file = new File("D:/tdsc/驻地食材坐标.txt");
        public static List<String> zbList = null;
//        public static void main(String[] args) {
//                System.out.println(readFileByLines());
//        }
        @Override
        public void run() {
                MFFrame.textArea.append(new Date() + "    读取坐标文件信息中.....\n");
                zbList = readFileByLines();
                MFFrame.textArea.append(new Date() + "    读取坐标文件信息完成!\n");
                MFFrame.textArea.append(new Date()+":    5秒后开始捡食材,请切换到游戏界面!\n");
                for (int i = 0;i<zbList.size();i++){
                        if(i==0){
                                try {
                                        Thread.sleep(5000);
                                        robot.keyPress(KeyEvent.VK_M);
                                        robot.keyRelease(KeyEvent.VK_M);
                                        Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                        e.printStackTrace();
                                }
                                pick(zbList.get(0),zbList.get(0),i);
                        }else{
                                pick(zbList.get(i-1),zbList.get(i),i);
                        }
                }
                //结束线程清空
                MFFrame.t = null;
                MFFrame.tKey = 1;
                System.out.println("驻地结束\n");
                MFFrame.textArea.append(new Date()+":    驻地结束\n");
        }
        //捡食材 根据前一个和当前的坐标计算走路时间
        public void pick(String beforeCoo,String nowCoo,int i){
                //计算走路时间
                String[] srrBefore = beforeCoo.split(",");
                String[] srrNow = nowCoo.split(",");
                System.out.println(Integer.parseInt(srrBefore[0]));
                System.out.println(Integer.parseInt(srrNow[0]));
                //x坐标差值
                int xCz = Math.abs(Integer.parseInt(srrBefore[0])-Integer.parseInt(srrNow[0]));
                //y坐标差值
                int yCz = Math.abs(Integer.parseInt(srrBefore[1])-Integer.parseInt(srrNow[1]));
                System.out.println("坐标差值:" + (xCz+yCz));
                //移动鼠标到x点,点击
                try {
                        Thread.sleep(600);
                        robot.mouseMove(710,22);
                        Thread.sleep(600);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        Thread.sleep(600);
                        //将坐标字符串转换为char数组   输入X坐标
                        char[] xrr = srrNow[0].toCharArray();
                        for (char c: xrr) {
                                robot.keyPress(c);
                                robot.keyRelease(c);
                        }
                        //TAB
                        Thread.sleep(600);
                        robot.keyPress(KeyEvent.VK_TAB);
                        robot.keyRelease(KeyEvent.VK_TAB);
                        Thread.sleep(600);
                        //输入Y坐标
                        char[] yrr = srrNow[1].toCharArray();
                        for (char cc: yrr) {
                                robot.keyPress(cc);
                                robot.keyRelease(cc);
                        }
                        //移动鼠标到寻路按钮,点击 并等待
                        Thread.sleep(600);
                        robot.mouseMove(883,22);
                        //连点两次寻路防止出现问题
                        Thread.sleep(500);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        Thread.sleep(500);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        //等待寻路睡眠
                        if((xCz+yCz) <30){
                                Thread.sleep(5300);
                        }
                        if((xCz+yCz) >=30){
                                Thread.sleep(7200);
                        }
                        if((xCz+yCz) >=40){
                                Thread.sleep(13000);
                        }
                        //按下F捡食材,等待3.5秒
                        robot.keyPress(KeyEvent.VK_F);
                        robot.keyRelease(KeyEvent.VK_F);
                        Thread.sleep(3500);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                //完成,下一次
                MFFrame.textArea.append(new Date() + "    第"+(i+1)+"次完成,坐标("+srrNow[0]+","+srrNow[1]+")\n");
        }
        /**
         * 以行为单位读取文件，常用于读面向行的格式化文件
         */
        public static List<String> readFileByLines() {
                BufferedReader reader = null;
                List<String> arr = new ArrayList<>();
                try {
                        System.out.println("以行为单位读取文件内容，一次读一整行：");
                        reader = new BufferedReader(new FileReader(file));
                        String tempString = null;
                        int line = 1;
                        // 一次读入一行，直到读入null为文件结束
                        while ((tempString = reader.readLine()) != null) {
                                // 显示行号
                                System.out.println("line " + line + ": " + tempString);
                                arr.add(tempString);
                                line++;
                        }
                        reader.close();
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        if (reader != null) {
                                try {
                                        reader.close();
                                } catch (IOException e1) {
                                }
                        }
                }
                try {
                        Thread.sleep(1000);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                return arr;
        }


}
